﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Text.RegularExpressions;

namespace ForestryEnterprise
{
    public class Subinvoice
    {
        private int amount;
        private int buyType;//1:m3 2:prm
        private int treeID;
        private float cost;

        public Subinvoice(int am, int buy, int tree)
        {
            this.amount = am;
            this.buyType = buy;
            this.treeID = tree;

            //get price from table depending on the buy type 1 for m3 and 2 for prm

            string statement = "select p_m3 as price from Pricelist where tree_id = @tID";
            
            if (buy == 2)
            {
                Regex.Replace(statement, "p_m3", "p_prm"); 
            }
            
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@tID", tree);
            connection.Open();
            OleDbDataReader reader = command.ExecuteReader();
            reader.Read();
            this.cost = float.Parse(reader["price"].ToString()); // nema ceni za site drva
            reader.Close();
            connection.Close();
        }

        public float Cost
        {
            get
            {
                return this.cost;
            }
        }

        public int Amount
        {
            set
            {
                this.amount = value;
            }
            get
            {
                return this.amount;
            }
        }

        public int BuyType
        {
            get
            {
                return this.buyType;
            }
        }

        public int TreeID
        {
            get
            {
                return this.treeID;
            }
        }
    }
}